package cn.wizzer.app.sys.modules.services.impl;


import cn.wizzer.app.sys.modules.models.SysIpWhiteList;
import cn.wizzer.app.sys.modules.services.SysIpWhiteListService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.plugins.wkcache.annotation.CacheDefaults;
import org.nutz.plugins.wkcache.annotation.CacheRemoveAll;
import org.nutz.plugins.wkcache.annotation.CacheResult;

import java.util.List;
import java.util.stream.Collectors;

@IocBean(args = {"refer:dao"})
@Service(interfaceClass = SysIpWhiteListService.class)
@CacheDefaults(cacheName = "sys_ip_white_list")
public class SysIpWhiteListServiceImpl extends BaseServiceImpl<SysIpWhiteList> implements SysIpWhiteListService {

    public SysIpWhiteListServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    @CacheResult
    public List<String> queryAll() {
        return this.query(Cnd.where("disabled", "=", false))
                .stream()
                .map(SysIpWhiteList::getIp)
                .collect(Collectors.toList());
    }

    @Override
    @CacheRemoveAll
    public void clearCache() {

    }

    @Override
    public void add(SysIpWhiteList ip) {
        this.insert(ip);
        this.clearCache();
    }

    @Override
    public void del(String id) {
        this.delete(id);
        this.clearCache();
    }

    @Override
    public void update(SysIpWhiteList ip) {
        this.updateIgnoreNull(ip);
        this.clearCache();
    }

}
